#!/usr/bin/python

import getopt
import sys

def usage():
    print "Do not use this program directly. Use wizard indead!"
    print "Mandatory agruments:"
    print "\t-c configfile, --config=configfile"
    print "\t-m modulename, --module=modulename"
    print "Other arguments:"
    print "\t-h, --help"
    print "List of supported modules:"
    print "0) usb"
    print "\t -s sectionname, --section=sectioname"
    print "\t -v vendorhex, --vendor=vendorhex"
    print "\t -p producthex, --product=producthex"
    
def hasConfigSection(section, configFile):
    print section
    print configFile
    import ConfigParser
    config = ConfigParser.RawConfigParser()
    config.read(configFile)
    bool = config.has_section(section)
    del config
    if bool:
        return True
    else:
        return False

if len(sys.argv) < 2:
    print "Wrong options. Type " + sys.argv[0] + " -h for help."
    sys.exit(2)

try:
    opts, extraparams = getopt.getopt(sys.argv[1:], "hc:m:s:v:p:", ["help", "config=", "section=", "module=", "vendor=", "product"])
except getopt.GetoptError, err:
    print str(err)
    usage()
    sys.exit(2)

    
module = None
section = None
product = None
vendor = None
config = None

for o, a in opts:
    if o in ("-h", "--help"):
        usage()
        sys.exit(1)
    elif o in ("-m", "--module"):
        module = a
    elif o in ("-c", "--config"):
        config = a
        print config
    elif o in ("-s", "--section"):
        section = a        
    elif o in ("-p", "--product"):
        product = a
    elif o in ("-v", "--vendor"):
        vendor = a
    else:
        usage()
        sys.exit(2)
        
if module == None:
    print "Please type a module name"
    sys.exit(2)
if config == None:
    print "No config file given"
    sys.exit(2)

elif module == "usb":
    if section == None or vendor == None or product == None:
        print "Please fill an extra arguments"
        sys.exit(2)
    else:
        if hasConfigSection(section, config):
            pass
        else:
            print "No such config section!"
            sys.exit(2)
        
        #Let's create config parser and pass whole shit to proper module
        from config.ParseUSBConfig import ParseUSBConfig
        configParser = ParseUSBConfig(config, section)
        from module.usb.USBModule import USBModule
        usbModule = USBModule(configParser, vendor, product)
        #start program here
else:
    print "No such module!"
    sys.exit(2)
